package org.rqb.ui.button;

import javax.swing.*;
import java.awt.*;

public class MyTextureButton extends JButton {

    private Color normalColor = new Color(70, 130, 180); // 🌟 默认颜色，可自定义
    private Color hoverColor = new Color(100, 160, 210); // 🌟 鼠标悬停颜色

    public MyTextureButton(String text) {
        super(text);

        this.setOpaque(false);
        this.setContentAreaFilled(false);
        this.setFocusPainted(false);
        this.setForeground(Color.WHITE);
        this.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
        this.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));

        this.setUI(new javax.swing.plaf.basic.BasicButtonUI() {
            @Override
            public void paint(Graphics g, JComponent c) {
                Graphics2D g2 = (Graphics2D) g.create();
                JButton b = (JButton) c;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 根据鼠标状态选择颜色
                Color baseColor = b.getModel().isRollover() ? hoverColor : normalColor;

                // 渐变背景
                g2.setPaint(new GradientPaint(0, 0, baseColor.brighter(), 0, b.getHeight(), baseColor.darker()));
                g2.fillRoundRect(0, 0, b.getWidth(), b.getHeight(), 20, 20);

                // 边框
                g2.setStroke(new BasicStroke(1f));
                g2.setColor(baseColor.darker().darker());
                g2.drawRoundRect(0, 0, b.getWidth() - 1, b.getHeight() - 1, 20, 20);

                // 绘制文字
                FontMetrics fm = g2.getFontMetrics();
                Rectangle r = new Rectangle(0, 0, b.getWidth(), b.getHeight());
                int textX = (r.width - fm.stringWidth(b.getText())) / 2;
                int textY = (r.height - fm.getHeight()) / 2 + fm.getAscent();
                g2.setColor(b.getForeground());
                g2.drawString(b.getText(), textX, textY);

                g2.dispose();
            }
        });

        // 鼠标进入/离开动态效果
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent e) {
                MyTextureButton.this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                MyTextureButton.this.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
                MyTextureButton.this.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent e) {
                MyTextureButton.this.setFont(new Font("Microsoft YaHei", Font.BOLD, 18));
                MyTextureButton.this.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
            }

            @Override
            public void mousePressed(java.awt.event.MouseEvent e) {
                MyTextureButton.this.setLocation(MyTextureButton.this.getX(), MyTextureButton.this.getY() + 2);
            }

            @Override
            public void mouseReleased(java.awt.event.MouseEvent e) {
                MyTextureButton.this.setLocation(MyTextureButton.this.getX(), MyTextureButton.this.getY() - 2);
            }
        });
    }

    // 可选：提供方法修改按钮颜色
    public void setNormalColor(Color normalColor) {
        this.normalColor = normalColor;
        this.repaint();
    }

    public void setHoverColor(Color hoverColor) {
        this.hoverColor = hoverColor;
        this.repaint();
    }
}
